<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <select-address
          v-model="params.address_code"
          @change="getData()"
          placeholder="地址查询"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.agency_name"
          @keyup.enter.native="getData()"
          placeholder="机构名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getData()" :loading="loading">查询</el-button>
        <el-button icon="el-icon-plus" :disabled="newable" @click="newBank">新增</el-button>
      </el-form-item>
    </el-form>
    <c-table border class="table-sm" v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <div class="text-truncate" :title="scope.row.address">{{ scope.row.address }}</div>
      </template>
      <template v-slot:status="scope">
        <span class="badge" :class="`badge-${scope.row.access_style}`">
          {{ scope.row.access_label }}
        </span>
      </template>
    </c-table>
    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>
    <select-access-bank-dialog
      :visible.sync="newVisible"
      width="80%"
      :disabled-node="disabledNode"
      v-model="newBankIds"
      @save="handleSubmit"
    ></select-access-bank-dialog>

    <edit-org
      :visible.sync="editVisible"
      @success="getData(params.index)"
      orgType="1"
      :formData="bankInfo"
      :tableId="bankId"
    ></edit-org>
  </div>
</template>

<script>
import SelectAddress from '@/components/select/SelectAddress'
import { mapGetters } from 'vuex'
import SelectAccessBankDialog from '@/credit/components/select/SelectAccessBankDialog'
import EditOrg from '@/credit/views//org/NewOrg'
export default {
  components: {
    SelectAddress,
    SelectAccessBankDialog,
    EditOrg,
  },
  data() {
    return {
      params: {
        index: 1,
        size: 15,
        'agency_type.eq': 1,
        address_code: '',
        agency_name: '',
      },
      order: {
        agency_create_time: 'desc',
      },
      loading: false,
      tableData: [],
      tableName: 'credit_agency_access_bank_view',
      tableColumns: [
        { attrs: { label: '机构名称', prop: 'agency_name' } },
        { attrs: { label: '地址', prop: 'address' }, slot: 'address' },
        { attrs: { label: '联系人', prop: 'contact' } },
        { attrs: { label: '联系电话', prop: 'mobile_no' } },
        {
          attrs: { label: '操作', align: 'center' },
          operations: [
            {
              content: '修改机构信息',
              icon: 'fa fa-edit',
              event: this.editCredit,
            },
          ],
        },
      ],
      totalCount: 0,
      newVisible: false,
      newBankIds: [],

      editVisible: false,
      bankInfo: {},
      bankId: '',

      disabledNode: [],
      newable: true,
    }
  },
  computed: {
    ...mapGetters(['clientId', 'user']),
  },
  created() {
    this.params['service_id.eq'] = this.clientId
    this.getDisNode()
    this.getData()
  },
  methods: {
    handleSubmit() {
      if (!this.newBankIds.length) {
        return this.$message.warning('请先选择金融机构')
      }
      const data = []
      this.newBankIds.forEach(item => {
        data.push({
          service_id: this.clientId,
          access_agency_id: item,
          access_status: 1,
        })
      })
      this.$msgbox({
        title: '提示',
        message: `确认新增所选机构 ？`,
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        beforeClose: async (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonText = '操作中...'
            instance.confirmButtonLoading = true
            await this.$api
              .addBatch(this.$serviceName, 'credit_agency_access', data)
              .then(() => {
                this.$message.success('添加成功')
                this.newBankIds = []
                this.newVisible = false
                this.getData()
              })
              .catch(() => {
                this.$message.error('添加失败')
              })
            instance.confirmButtonLoading = false
            instance.confirmButtonText = '确定'
            done()
          } else {
            done()
          }
        },
      }).catch(() => {})
    },
    editCredit(data) {
      this.bankInfo = data ? { ...data } : {}
      this.bankId = data ? data.credit_agency_info_id : ''
      this.editVisible = true
    },
    async getData(index = 1) {
      this.params.index = index
      this.loading = true
      this.$api
        .getData(this.$serviceName, this.tableName, this.params, this.order)
        .then(res => {
          const data = (res && res.data) || {}
          this.tableData = (data.data || []).map(this.dataAdapter)
          this.totalCount = data.totalCount
        })
        .finally(() => {
          this.loading = false
        })
    },
    getDisNode() {
      const params = {
        'agency_type.eq': 1,
        'service_id.eq': this.clientId,
      }
      this.$api.getData(this.$serviceName, this.tableName, params).then(res => {
        const data = (res && res.data) || []
        this.disabledNode = data.map(item => {
          return item.access_agency_id
        })
        this.newable = false
      })
    },
    newBank() {
      this.newVisible = true
    },
    dataAdapter(item) {
      item.mobile_no = item.mobile_no || ''
      return item
    },
  },
}
</script>
<style lang="scss" scoped></style>
